package org.bot.server.init;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.bot.client.driver.api.client.ClientOpenApi;
import org.bot.client.driver.api.server.ServerReceiveClientData;
import org.bot.server.event.entity.BotEvent;
import org.bot.server.event.mapper.BotEventMapper;
import org.bot.server.utils.Mode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @author 沧月
 * @create 0002 2023/11/2 18:07
 * @description <本地启动模式收集Event>
 */
@Component
public class LocationEventCommandLineRunner implements CommandLineRunner {

    @Value("${bot.mode}")
    private String mode;

    @Autowired
    ClientOpenApi serverDriver;

    @Autowired
    ServerReceiveClientData serverClientInfo;

    @Autowired
    BotEventMapper botEventMapper;

    @Override
    public void run(String... args) throws Exception {
        String mode = Mode.LOCATION.getMode();
        if (!mode.equals(this.mode)) {
            return;
        }

        UpdateWrapper<BotEvent> wrapper = new UpdateWrapper<>();
        wrapper.eq("clientId", mode);
        botEventMapper.delete(wrapper);

        JSONObject eventList = serverDriver.getEventList(null);
        JSONArray data = eventList.getJSONArray("data");
        serverClientInfo.clientEvent(mode, data);
    }
}
